/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       imageloader.cpp
 * Author:     karooolek
 * Created on: 2009-05-12
 *
 **********************************************************************************************************************/

#include "imageloader.h"
#include "bmploader.h"
#include "tgaloader.h"
#include "pngloader.h"

namespace mGameEngine
{

namespace Graphics
{

Image *ImageLoader::loadImageFromFile(const String &filename)
{
    ImageLoader ld(filename);
    ld.load();
    return ld.getImage();
}

ImageLoader::ImageLoader() : ImageFactory(), FileLoader()
{
}

ImageLoader::ImageLoader(const String &filename) :
    ImageFactory(), FileLoader(filename)
{
}

ImageLoader::~ImageLoader()
{
}

void ImageLoader::load()
{
    // begin loading
    _state = 0.0f;

    if(_extension == "bmp")
    {
        BMPLoader loader(_filename);
        loader.load();
        _image = loader.getImage();
        _state = loader.getLoadingState();
    }
    else if(_extension == "tga")
    {
        TGALoader loader(_filename);
        loader.load();
        _image = loader.getImage();
        _state = loader.getLoadingState();
    }
    else if(_extension == "png")
    {
        PNGLoader loader(_filename);
        loader.load();
        _image = loader.getImage();
        _state = loader.getLoadingState();
    }
    else
    {
        return;
    }
}

void ImageLoader::unload()
{
    // no image to unload
    if(!_image)
    {
        _state = 0.0f;
        return;
    }

    // free data
    delete[] _image->getData();

    // end unloading
    _state = 0.0f;
}

}
}

